package com.ruizhong.centercontroll.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruizhong.centercontroll.dao.TPPTMapper;
import com.ruizhong.centercontroll.entity.TCommand;
import com.ruizhong.centercontroll.entity.TPPT;

import com.ruizhong.centercontroll.entity.TWebPage;
import com.ruizhong.centercontroll.service.TCommandService;
import com.ruizhong.centercontroll.service.TPPTService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class TPPTServiceImpl extends ServiceImpl<TPPTMapper, TPPT> implements TPPTService {
    
    @Autowired
    private TCommandService tCommandService;
    
    
    @Override
    public void saveNewPPTList(List<Map<String, Object>> pptList, Integer pageId, boolean isNew) {
        final HashMap<Integer, List<Map<String, Object>>> pptIdMapInitCommandList = new HashMap<>();
        deletePptsByPageId(pageId);
        pptList.stream().forEach(pptOB -> {
            TPPT tPpt = new TPPT();
            tPpt.setPageId(pageId);
            tPpt.setW(Double.parseDouble(pptOB.get("w").toString()));
            tPpt.setH(Double.parseDouble(pptOB.get("h").toString()));
            tPpt.setX(pptOB.get("x") == null?0:Double.parseDouble(pptOB.get("x").toString()));
            tPpt.setY(pptOB.get("y") == null?0:Double.parseDouble(pptOB.get("y").toString()));
            tPpt.setZ(Integer.parseInt(pptOB.get("z").toString()));
            Object displayVariableName = pptOB.get("displayVariableName");
            Object displayVariableValue = pptOB.get("displayVariableValue");
            tPpt.setDisplayVariableName(displayVariableName == null ? null : displayVariableName.toString());
            tPpt.setDisplayVariableValue(displayVariableValue == null ? null : displayVariableValue.toString());
            Object conditionShow = pptOB.get("conditionShow");
            tPpt.setConditionShowData(conditionShow == null ? null : (Boolean) conditionShow ? 1 : 0);
            Object pptUrlVariable = pptOB.get("pptUrlVariable");
            tPpt.setPptUrlVariable(pptUrlVariable == null ? null : pptUrlVariable.toString());
            List<Map<String,Object>> initCommandList = null;
            Map logicGraph = (Map) pptOB.get("logicGraph");
            if (!isNew){
                initCommandList = (List<Map<String, Object>>) pptOB.get("initCommandList");
            }else{
                ObjectMapper mapper = new ObjectMapper();
                try {
                    String logicGraphString = mapper.writeValueAsString(logicGraph);
                    tPpt.setLogicGraphData(logicGraphString);
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                }
                Map<String, List> typeMapCommandList = TCommandServiceImpl.getCommandsByGraph(logicGraph);
                initCommandList = typeMapCommandList.get("initCommandList");
            }
            save(tPpt);
            pptIdMapInitCommandList.put(tPpt.getId(), initCommandList);
        });
        Runnable runnable = new Runnable() {
            public void run() {
                tCommandService.saveCommandList("PPT", "init", pptIdMapInitCommandList);
            }
        };
        new Thread(runnable).start();
    }

    private void deletePptsByPageId(Integer pageId) {
        QueryWrapper<TPPT> deleteWrapper = new QueryWrapper<>();
        deleteWrapper.lambda().eq(TPPT::getPageId, pageId);
        remove(deleteWrapper);
    }

    @Override
    public List<TPPT> getPPTsByPageId(Integer pageId, Double pageWidth, Double pageHeight, Double aimPageWidth, Double aimPageHeight, Boolean display) throws JsonProcessingException {
        List<TPPT> ppts = lambdaQuery()
                .eq(TPPT::getPageId, pageId)
                .list();
        for (TPPT ppt : ppts) {
            if (aimPageHeight != -1 && aimPageWidth != -1) {
                ppt.setW(Math.floor((ppt.getW() * aimPageWidth) / pageWidth));
                ppt.setH(Math.floor((ppt.getH() * aimPageHeight) / pageHeight));
                ppt.setX(Math.floor((ppt.getX() * aimPageWidth) / pageWidth));
                ppt.setY(Math.floor((ppt.getY() * aimPageHeight) / pageHeight));
            }
            ppt.setConditionShow(ppt.getConditionShowData() == null ? false : ppt.getConditionShowData() == 1);
            if (!display){
                String logicGraphData = ppt.getLogicGraphData();
                if (!StringUtils.isEmpty(logicGraphData)){
                    ObjectMapper mapper = new ObjectMapper();
                    Map map = mapper.readValue(logicGraphData, Map.class);
                    ppt.setLogicGraph(map);
                }
                List<TCommand> initCommands = tCommandService.getCommandListByMain(ppt.getId(), ppt.getType(), "init");
                initCommands = tCommandService.fillTCommands(initCommands);
                ppt.setInitCommandList(initCommands);
            }
        }
        return ppts;
    }

    @Override
    public List<TPPT> getPPTIdsByPageId(Integer pageId) {
        List<TPPT> list = lambdaQuery()
                .select(TPPT::getId)
                .eq(TPPT::getPageId, pageId)
                .list();
        return list;
    }
}
